import time
from appium import webdriver
from appium.options.android import UiAutomator2Options
from appium.webdriver.common.appiumby import AppiumBy

from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.wait import WebDriverWait

from Common.read_verifycode import verify_code
from config import Basedir

# com.tpshop.malls/com.tpshop.malls.SPMainActivity
# 配置设备能力和测试参数
#
capabilities = dict(
    platformName='Android',  # 指定移动平台为Android
    automationName='uiautomator2',  # 使用UI Automator 2框架（Android专属）
    deviceName='Android',  # 设备名称（通常使用实际连接的设备名称）
    appPackage='com.android.settings',  # 要测试的应用包名（这里是系统设置）
    appActivity='.Settings',  # 应用的主活动（入口Activity）
    # noReset=True  # 不清除APP里的原有的数据
)

# 将字典转换为Options对象
options = UiAutomator2Options().load_capabilities(capabilities)

# Appium服务器地址（默认本地服务器，端口4723）
appium_server_url = 'http://localhost:4723'
# 创建Appium WebDriver实例
driver = webdriver.Remote(appium_server_url, options=options)
driver.implicitly_wait(10)
"""----------------------------------------------------------------------------------------------------------"""


# 封装使用显示等待寻找元素
def find_el(locator):
    try:
        el = WebDriverWait(driver, 10, 1).until(
            EC.presence_of_element_located(locator))
        return el
    except TimeoutException:
        return TimeoutException


"""----------------------------------------------------------------------------------------------------------"""
driver.terminate_app('com.android.settings')
# 打开Tpshop
find_el((AppiumBy.XPATH, '//android.widget.TextView[@text="TP商城单商户"]')).click()
# 进入我的
find_el((AppiumBy.ANDROID_UIAUTOMATOR, 'new UiSelector().resourceId("com.tpshop.malls:id/mine_img")')).click()

# 进入登录页
find_el((AppiumBy.ID, 'com.tpshop.malls:id/head_img')).click()


# 封装登录操作
def login(username, password):
    # 输入账号密码
    find_el((AppiumBy.ANDROID_UIAUTOMATOR, 'new UiSelector().text("请输入账号")')).send_keys(username)
    find_el((AppiumBy.ANDROID_UIAUTOMATOR, 'new UiSelector().text("请输入密码")')).send_keys(password)
    # 勾选协议
    find_el((AppiumBy.CLASS_NAME, 'android.widget.Button')).click()
    # time.sleep(2)
    # 点击登录
    find_el((AppiumBy.XPATH, '//android.widget.TextView[@resource-id="com.tpshop.malls:id/login_tv"]')).click()


# 封装注册操作
def register(phone, password):
    # 进入注册页
    find_el((AppiumBy.ANDROID_UIAUTOMATOR, 'new UiSelector().text("手机快速注册")')).click()
    # 输入手机号
    find_el((AppiumBy.ANDROID_UIAUTOMATOR, 'new UiSelector().text("请输入手机号码")')).send_keys(phone)
    # 截图获取验证码
    find_el((AppiumBy.ID, 'com.tpshop.malls:id/random_code_img')).screenshot(Basedir + '/Img/register.png')
    code = verify_code(Basedir + '/Img/register.png')
    # 输入验证码
    find_el((AppiumBy.ANDROID_UIAUTOMATOR, 'new UiSelector().text("请输入验证码")')).send_keys(code)
    # 输入密码
    find_el((AppiumBy.ANDROID_UIAUTOMATOR, 'new UiSelector().text("请输入新密码")')).send_keys(password)
    find_el((AppiumBy.ANDROID_UIAUTOMATOR, 'new UiSelector().text("请输入确认密码")')).send_keys(password)
    # 点击注册按钮
    find_el((AppiumBy.ID, 'com.tpshop.malls:id/register_tv')).click()


# 个人资料页
def personal_data():

    find_el((AppiumBy.ID, 'com.tpshop.malls:id/head_img')).click()


login('13800138006', '123456')
# register('18688887777', '123456')

# driver.quit()
